package com.fenqing.leetcode.code.t200_300;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.Arrays;

/**
 * @author fenqing
 */
public class Topic209 extends Topic {
    /**
     * 构造器
     *
     * @param input1 输入
     * @param input2 输入
     */
    public Topic209(String input1, String input2) {
        super(input1, input2);
    }

    @Execute
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int ans = Integer.MAX_VALUE;
        int[] sums = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            sums[i] = sums[i - 1] + nums[i - 1];
        }
        for (int i = 1; i <= n; i++) {
            int t = target + sums[i - 1];
            int bound = Arrays.binarySearch(sums, t);
            if (bound < 0) {
                bound = -bound - 1;
            }
            if (bound <= n) {
                ans = Math.min(ans, bound - (i - 1));
            }
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }

    public static void main(String[] args) {
        Topic209 topic209 = new Topic209("7", "[2,3,1,2,4,3]");
        Integer run = topic209.run();
        System.out.println(run);
    }
}
